package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.TypeVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Lenovo
 * @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
 * @createDate 2025-07-19 08:43:08
 */
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
        implements TbTypeService {

    @Autowired
    TbTypeMapper tbTypeMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R getTypeList(TypeVo typeVo) {
        LambdaQueryWrapper<TbType> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(typeVo.getKeyWord())) {
            wrapper.like(TbType::getTypeName, typeVo.getKeyWord());
        }
        if (typeVo.getStatusId() != null) {
            wrapper.eq(TbType::getTypeStatus, typeVo.getStatusId());
        }
        if(StringUtils.isNotEmpty(typeVo.getKeyWord())||typeVo.getStatusId() != null){
            List<TbType> tbTypeList = tbTypeMapper.selectList(wrapper);
            return R.OK(tbTypeList);
        }
        List<TbType> tbTypeList = tbTypeMapper.selectList(wrapper);
        List<Tree<String>> treeList = TreeUtil.build(tbTypeList, "0", (type, tree) -> {
            tree.setId(type.getTypeId()+"");
            tree.putExtra("typeId",type.getTypeId());
            tree.putExtra("typeName",type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeStatus", type.getTypeStatus());
        });
        return R.OK(treeList);
    }

    @Override
    public R addType(TbType tbType) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(404, "请先登录");
        }
        String userName = TokenUtil.userName(token);
        TbType type = tbTypeMapper.selectOne(
                new LambdaQueryWrapper<TbType>().eq(TbType::getTypeName, tbType.getTypeName())
        );
        if (type != null) {
            return R.ERROR(500, "该类别已经存在");
        }

        if (tbType.getTypePid() == 0 || tbType.getTypePid() == null) {
            tbType.setCreateBy(userName);
            tbType.setCreateTime(new Date());

            tbTypeMapper.insert(tbType);
        } else {
            tbType.setTypePid(tbType.getTypePid());
            tbType.setCreateBy(userName);
            tbType.setCreateTime(new Date());

            tbTypeMapper.insert(tbType);
        }
        return R.OK();
    }

    @Override
    public R uploadImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }
}




